package jxta.chat.util;

import java.io.File;
import java.net.URI;

/**
 * A class that holds all the system constants 
 */
public class Constants {

	public final static String MESSAGE_TYPE_SPACE = "MsgType";
	public final static String MESSAGE_NAME_SPACE = "msg";
	public final static String NAME_ATTRIBUTE = "Name";
	public final static String CHAT_REQUEST_ATTRIBUTE = "request";
	public final static String CHAT_RESPONSE_ATTRIBUTE = "response";
	
	/**
	 * An enumeration of all the values in the message parameters 
	 */
	public enum MessageParamsKeys{
		NAME("PeerName"), PIPE_ADV("PipeAdv"), MESSAGE("msg"), REQUEST("request"), RESPONSE("response");
		
		private String name;
		
		private MessageParamsKeys(String name){
			this.name = name;
		}
	
		@Override
		public String toString(){
			return getName();
		}
		
		public String getName(){
			return name;
		}
	}
	
	/**
	 * An enumeration of all the message types 
	 */
	public enum MessageTypes{
		REGULAR("Regular"), LOGIN("Login"), LOGOUT("LogOut"), VOIP_CHAT("voChat"), USER_DISCOVERED("discover");
		
		private String name;
		
		private MessageTypes(String name){
			this.name = name;
		}
	
		@Override
		public String toString(){
			return getName();
		}
		
		public String getName(){
			return name;
		}
	}
	
	/**
	 * An enumeration of all the responses for a chat invitation
	 */
	public enum ChatInvitaionResponseVals{
		PEER_UNREACHABLE("unreachable"), BUSY("busy"), ACCEPT("accept"), REFUSE("refuse");
		
		private String name;
		
		private ChatInvitaionResponseVals(String name){
			this.name = name;
		}
	
		@Override
		public String toString(){
			return getName();
		}
		
		public String getName(){
			return name;
		}		
		
		public static ChatInvitaionResponseVals getValue(String val){
			for (ChatInvitaionResponseVals val2 : values()){
				if (val2.toString().equalsIgnoreCase(val)){
					return val2;
				}
			}
			
			return null;
		}
	}
	
	/**
	 * An enumeration of all the responses for a chat invitation
	 */
	public enum ChatRequestVals{
		CALL_REQUEST("call"), INVITATION_REQUEST("invite");
		
		private String name;
		
		private ChatRequestVals(String name){
			this.name = name;
		}
	
		@Override
		public String toString(){
			return getName();
		}
		
		public String getName(){
			return name;
		}		
		
		public static ChatRequestVals getValue(String val){
			for (ChatRequestVals val2 : values()){
				if (val2.toString().equalsIgnoreCase(val)){
					return val2;
				}
			}
			
			return null;
		}
	}
	
	/**
	 * Constant for the jxta home system parameter
	 */
	public static final String MYJXTA = "JXTA_HOME";
	
	public static final String DEFAULT_HOME = "user.home";
	
	private static Constants mInstance = null;
	
	private String instanceName;
	
	/**
     * Home directory for jxta application
     */
    private static URI myjxta = null;
	
	private Constants(String name){
		this.instanceName = name;
		
		initHome();
	}
	
	/**
	 * Initialize the jxta home uri
	 */
	private void initHome() {
     
        String mh = System.getProperty(MYJXTA);

        mh = mh != null ? mh.trim() : null;

        if (mh == null || mh.length() == 0) {
        	
            // If it is not found use the user home directory
            // and create the missing configuration directory

            String uh = System.getProperty(DEFAULT_HOME);
            
            mh = instanceName.startsWith(uh) ? instanceName : uh + File.separator + instanceName;
        }
        else{
        	mh = instanceName.startsWith(mh) ? instanceName : mh + File.separator + instanceName;
        }
        
        System.setProperty(MYJXTA, mh);

        // Create the home directory
        File f = new File(mh);

        if (!f.exists()) {
            f.mkdirs();
        }
        
        myjxta = f.toURI();
    }
	
	public static Constants getInstance(){
		return mInstance;
	}
	
	public static Constants getInstance(String instanceName){
		if (mInstance == null){
			mInstance = new Constants(instanceName);
		}
		
		return mInstance;
	}
	
	public String getPeerName(){
		return this.instanceName; 
	}
	
	/**
	 * @return URI for the jxta home 
	 */
	public URI getJxtaHome(){
		return myjxta;
	}
	
}
